/**
 * Package:com.afteryuan.server.hanlder.core;
 * $Id$
 * Copyright(c) 2001-2005 www.afteryuan.com
 */
package com.afteryuan.server.handler.core;

import com.afteryuan.server.handler.Handler;
import com.afteryuan.server.command.up.CommandUp;
import com.afteryuan.server.command.up.core.QuitRoomUp;
import com.afteryuan.server.command.down.core.PolicyDown;
import com.afteryuan.server.command.down.core.QuitRoomDown;
import com.afteryuan.server.ConnectSession;
import com.afteryuan.server.config.Configuration;
import com.afteryuan.server.service.SpriteService;
import com.afteryuan.server.data.SpritePool;
import com.afteryuan.server.data.Room;

/**
 * QuitRoomHandler 处理退出Room的请求
 * <p/>
 * 处理步骤:
 * 1.从Room中删除此Sprite;
 * 2.通知Room中其他人,此avatar离开Room
 * 3.通知所有人.,此avatar离开Room(可配置)
 * <p><a href="PingHandler.java.html"><i>View Source</i></a></p>
 *
 * @author <a href="mailto:afteryuan@afteryuan.com">$Author$</a>
 * @version $Revision$
 */
public class QuitRoomHandler implements Handler {
    private SpritePool spritePool;
    private SpriteService spriteService;

    public void setSpritePool(SpritePool spritePool) {
        this.spritePool = spritePool;
    }

    public void setSpriteService(SpriteService spriteService) {
        this.spriteService = spriteService;
    }

    public void process(ConnectSession session, CommandUp command) {
        QuitRoomUp cmd = (QuitRoomUp) command;

        //1.从Room中删除此Sprite;
        Room room = new Room(cmd.sceneId,cmd.sceneInstanceId);
        spritePool.removeFromRoom(room,cmd.sender);

        //2.通知Room中其他人,此avatar离开Room
        QuitRoomDown quit = new QuitRoomDown();
        quit.avatarId = cmd.sender;
        spriteService.broadcast(room,quit);
        
        //3.通知所有人.,此avatar离开Room(可配置)
        if(Configuration.getInstance().isBroadcastQuitRoom()){
            spriteService.broadcast(quit);        
        }

    }
}